#include <wx/bmpbuttn.h>
#include "wxsbitmapcombobox.h"

namespace {
  wxsRegisterItem<wxsBitmapComboBox> Reg( _T( "BitmapComboBox" ), _T( "位图组合框" ), wxsTWidget, _T( "标准" ), 50 );
  WXS_ST_BEGIN( wxsBitmapComboBoxStyles, _T( "wxBU_AUTODRAW" ) )
  WXS_ST_CATEGORY( "wxBitmapComboBox" )
  WXS_ST( wxCB_SIMPLE )
  WXS_ST( wxCB_SORT )
  WXS_ST( wxCB_READONLY )
  WXS_ST( wxCB_DROPDOWN )
  WXS_ST( wxTE_PROCESS_ENTER )
  WXS_ST_DEFAULTS()
  WXS_ST_END()
  WXS_EV_BEGIN( wxsBitmapComboBoxEvents )
  WXS_EVI( EVT_COMBOBOX, wxEVT_COMMAND_COMBOBOX_SELECTED, wxCommandEvent, Select )
  WXS_EVI( EVT_TEXT, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEvent, Text )
  WXS_EVI( EVT_TEXT_ENTER, wxEVT_COMMAND_TEXT_ENTER, wxCommandEvent, TextEnter )
  WXS_EV_END()
}

wxsBitmapComboBox::wxsBitmapComboBox( wxsItemResData* Data ):
  wxsWidget(
    Data,
    &Reg.Info,
    wxsBitmapComboBoxEvents,
    wxsBitmapComboBoxStyles ) {
  mImageList = _T( "<none>" );
  mItems.Clear();
}

void wxsBitmapComboBox::OnBuildCreatingCode() {
  int         i, n;
  wxString    ss, tt, vv;
  bool        ilist;
  if( GetLanguage() != wxsCPP ) {
    wxsCodeMarks::Unknown( _T( "wxsBitmapComboBox" ), GetLanguage() );
  }
  AddHeader( _T( "<wx/bmpcbox.h>" ), GetInfo().ClassName, hfInPCH );
  vv = GetVarName();
  Codef( _T( "%C(%W, %I, wxEmptyString, %P, %S, 0, NULL, %T, %V, %N);\n" ) );
  ilist = ( wxsImageListEditorDlg::FindTool( this, mImageList ) != NULL );
  for( i = 2; i < ( int )mItems.GetCount(); ++i ) {
    ss = mItems.Item( i );
    ParseComboItem( ss, tt, n );
    Codef( _T( "%s->Append(_T(\"%s\"));\n" ), vv.wx_str(), tt.wx_str() );
    if( ( ilist ) && ( n >= 0 ) ) {
      tt.Printf( _T( "%s->SetItemBitmap(%d, %s->GetBitmap(%d));\n" ), vv.wx_str(), i - 2, mImageList.wx_str(), n );
      AddEventCode( tt );
    };
  };
  AddEventCode( _T( "\n" ) );
  BuildSetupWindowCode();
}


wxObject* wxsBitmapComboBox::OnBuildPreview( wxWindow* Parent, long Flags ) {
  int                 i, n;
  wxBitmapComboBox    *combo;
  wxsImageList        *ilist;
  wxString            ss, tt;
  wxBitmap            bmp;
  combo = new wxBitmapComboBox( Parent, GetId(), _T( "" ), Pos( Parent ), Size( Parent ), 0, NULL, Style() );
  ilist = ( wxsImageList * ) wxsImageListEditorDlg::FindTool( this, mImageList );
  combo->Clear();
  for( i = 2; i < ( int )mItems.GetCount(); ++i ) {
    ss = mItems.Item( i );
    ParseComboItem( ss, tt, n );
    combo->Append( tt );
    if( ( ilist != NULL ) && ( n >= 0 ) ) {
      combo->SetItemBitmap( i - 2, ilist->GetPreview( n ) );
    }
  };
  return SetupWindow( combo, Flags );
}


void wxsBitmapComboBox::OnEnumWidgetProperties( M_Code_unused long Flags ) {
  static wxString         sImageNames[128];
  static const wxChar    *pImageNames[128];
  int                     i, n;
  wxString                ss, tt;
  wxArrayString           aa;
  FindAllImageLists( aa );
  n = aa.GetCount();
  if( n > 127 ) {
    n = 127;
  }
  for( i = 0; i < n; i++ ) {
    ss = aa.Item( i );
    sImageNames[i] = ss;
    pImageNames[i] = ( const wxChar * ) sImageNames[i];
  };
  pImageNames[n] = NULL;
  WXS_EDITENUM( wxsBitmapComboBox, mImageList, _T( "Image List" ), _T( "mImageList" ), pImageNames, _T( "<none>" ) )
  UpdateComboItemList();
  WXS_IMAGECOMBO( wxsBitmapComboBox, mItems, _T( "Combo Items" ), _T( "mItems" ) );
  WXS_ARRAYSTRING( wxsBitmapComboBox, mItems, _T( "Items as Text" ), _T( "mItemsText" ), _T( "item2" ) );
}



void wxsBitmapComboBox::FindAllImageLists( wxArrayString &aNames ) {
  int             i, n;
  wxsItemResData  *res;
  wxsTool         *tool;
  wxString        ss, tt;
  aNames.Clear();
  aNames.Add( _T( "<none>" ) );
  res = GetResourceData();
  n   = res->GetToolsCount();
  for( i = 0; i < n; i++ ) {
    tool = res->GetTool( i );
    ss   = tool->GetUserClass();
    if( ( ss == _T( "wxImageList" ) ) && ( n < 127 ) ) {
      ss = tool->GetVarName();
      aNames.Add( ss );
    };
  };
}



void wxsBitmapComboBox::UpdateComboItemList( void ) {
  int             i, n;
  wxString        ss, tt;
  wxArrayString   aa;
  aa.Clear();
  ss = GetVarName();
  aa.Add( ss );
  ss = mImageList;
  aa.Add( ss );
  n = mItems.GetCount();
  for( i = 2; i < n; i++ ) {
    ss = mItems.Item( i );
    aa.Add( ss );
  };
  mItems.Clear();
  n = aa.GetCount();
  for( i = 0; i < n; i++ ) {
    ss = aa.Item( i );
    mItems.Add( ss );
  };
  wxsImageListEditorDlg::FindTool( this, mImageList );
}


void wxsBitmapComboBox::ParseComboItem( wxString inSource, wxString &outItem, int &outIndex ) {
  int         i;
  long        ll;
  wxString    ss, tt;
  ss = inSource;
  i = ss.Find( _T( "," ) );
  if( i != wxNOT_FOUND ) {
    tt = ss.Left( i );
    ss.erase( 0, i + 1 );
    outItem = ss;
    outIndex = -1;
    if( tt.ToLong( &ll ) ) {
      outIndex = ll;
    }
  } else {
    outIndex = -1;
    outItem = ss;
  };
}
